<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      border: 1px solid black;
    }
  </style>
</head>

<body>
  <button id="beforebegin">beforebegin</button>
  <button id="afterbegin">afterbegin</button>
  <button id="beforeend">beforeend</button>
  <button id="afterend">afterend</button>

  <div>
    Hello world!
  </div>

  <script>
    const div = document.querySelector('div')

    function insert(position) {
      const text = `<p>${new Date().toString()}</p>`
      // 向元素的指定位置插入内容
      div.insertAdjacentHTML(position, text)
    }

    function beforebegin() {
      insert('beforebegin')
    }

    function afterbegin() {
      insert('afterbegin')
    }

    function beforeend() {
      insert('beforeend')
    }

    function afterend() {
      insert('afterend')
    }

    document.querySelector('#beforebegin').addEventListener('click', beforebegin)
    document.querySelector('#afterbegin').addEventListener('click', afterbegin)
    document.querySelector('#beforeend').addEventListener('click', beforeend)
    document.querySelector('#afterend').addEventListener('click', afterend)
  </script>
</body>

</html>